﻿namespace NewsMine.Utilities
{
    using System;
    using System.IO;
    using System.Net;

    public static class TempLogger
    {
        private static object syncObject = new object();
        private static object webExceptionFileLock = new object();

        public static string LogsBaseDirectory { get; set; }

        public static string GetFilepathFromCurrentLogsDirectory(string filename)
        {

            string logsDirectory = null;

            if (string.IsNullOrEmpty(LogsBaseDirectory))
            {
                LogsBaseDirectory = Path.Combine(Directory.GetCurrentDirectory(), @"logs\");
                //LogsBaseDirectory = Path.Combine(LogsBaseDirectory, @"logs\");
            }


            if (Directory.Exists(LogsBaseDirectory) == false)
            {
                Directory.CreateDirectory(LogsBaseDirectory);
            }

            return Path.Combine(LogsBaseDirectory, filename);

        }



        static TempLogger()
        {
           
        }
        public static void Log(string msg)
        {
            try
            {

                lock (syncObject)
                {
                    File.AppendAllText(GetFilepathFromCurrentLogsDirectory("WindowsAppErrors.txt"), "\n\r===============================\n\r" + DateTime.Now.ToString() + "\n\r" + msg);
                }
            }
            catch (Exception ex)
            {
                //todo:needs to implement.
            }
        }

        public static void Log(string msg, Exception ex)
        {
            try
            {
                if (ex != null)
                {
                    File.AppendAllText(GetFilepathFromCurrentLogsDirectory(ex.GetType().ToString() + ".txt"), "\n\r===============================\n\r" + DateTime.Now.ToString() + "\n\r" + msg + " \r\n" + ex.Message + ex.StackTrace);
                }
                lock (syncObject)
                {
                    File.AppendAllText(GetFilepathFromCurrentLogsDirectory("WindowsAppErrors.txt"), "\n\r===============================\n\r" + DateTime.Now.ToString() + "\n\r" + msg + " \r\n" + ex.Message + ex.StackTrace);
                }
            }
            catch (Exception ex1)
            {
                //todo:needs to implement.
            }
        }


        public static void LogCustom(string filePath, string msg)
        {
            try
            {
                lock (syncObject)
                {
                    File.AppendAllText(GetFilepathFromCurrentLogsDirectory(filePath), "\n\r" + DateTime.Now.ToString() + "\n\r" + msg);
                }
            }
            catch (Exception ex)
            {
                //todo:needs to implement.
            }
        }



        public static void LogWebException(string requestUrl, WebException ex)
        {

            lock (webExceptionFileLock)
            {
                string logLine = "{0}\t{1}\t{2}\t" + DateTime.UtcNow + "\t" + ex.Message;

                Uri uri = new Uri(requestUrl);

                if (ex is WebException)
                {
                    WebException we = (WebException)ex;
                    if (we.Status == WebExceptionStatus.ProtocolError)
                    {
                        var response = we.Response as HttpWebResponse;
                        if (response != null)
                        {
                            Console.WriteLine("HTTP Status Code: " + (int)response.StatusCode);
                            logLine = string.Format(logLine, (int)response.StatusCode, uri.Host, requestUrl);
                        }
                        else
                        {
                            logLine = string.Format(logLine, 0, uri.Host, requestUrl);
                        }
                    }
                    else
                    {
                        logLine = string.Format(logLine, 0, uri.Host, requestUrl);
                    }

                    try
                    {


                        File.AppendAllText(GetFilepathFromCurrentLogsDirectory("WebExceptionsSummary.txt"), "\n\r" + logLine);

                        File.AppendAllText(GetFilepathFromCurrentLogsDirectory("WebExceptionsDetails.txt"), "\n\r===============================\n\r" + DateTime.Now.ToString() + "\n\r" + ex.Message + " \r\n" + ex.Message + ex.StackTrace);
                    }
                    catch (Exception ex1)
                    {
                        //todo:needs to implement.
                    }
                }
            }
        }
    }
}

